package com.cuitrip.business.bill;

import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cuitrip.apiservice.OrderBusiness;
import com.cuitrip.apiservice.model.ApiManager;
import com.cuitrip.business.notice.model.FeeDescription;
import com.cuitrip.component.item.ItemLayout;
import com.cuitrip.component.statelayout.StateLayout;
import com.cuitrip.service.R;
import com.cuitrip.util.PlatformUtil;
import com.cuitrip.util.time.InputTime;
import com.cuitrip.util.time.OutputTime;
import com.cuitrip.util.time.TimeUtil;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragment;
import com.lab.network.config.CtResponseCode;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.utils.MessageUtils;
import com.lab.utils.NumberUtils;

import java.io.Serializable;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by MiJack on 2015/11/2.
 */
public class BillDetailFragment extends CustomUiFragment implements IProxyCallback {
    private static final String TYPE = "{TYPE}";
    private static final String PRICE = "{PRICE}";

    @Bind(R.id.layoutOrderPrice)
    ItemLayout layoutOrderPrice;
    @Bind(R.id.layoutPayFee)
    ItemLayout layoutTranFee;
    @Bind(R.id.layoutServiceFee)
    ItemLayout layoutServiceFee;
    @Bind(R.id.layoutMoney)
    ItemLayout layoutMoney;
    @Bind(R.id.layoutTripType)
    ItemLayout layoutTripType;
    @Bind(R.id.layoutTripTitle)
    ItemLayout layoutTripTitle;
    @Bind(R.id.layoutUserNick)
    ItemLayout layoutUserNick;
    @Bind(R.id.layoutTime)
    ItemLayout layoutTime;
    @Bind(R.id.moneyType)
    TextView moneyType;
    @Bind(R.id.moneyCount)
    TextView moneyCount;
    @Bind(R.id.state_layout)
    StateLayout mStateLayout;

    String billId;

    private ApiProxy mApiProxy = new ApiProxy(this);
    private ArrayList<FeeDescription> notice;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState, R.layout.fragment_bill_detail);
        ButterKnife.bind(this, view);
        billId = getArguments().getString("billId", null);
        if (billId == null) {
            mStateLayout.changeState(StateLayout.State.Empty);
            return view;
        }
        mStateLayout.changeState(StateLayout.State.Loading);
        showNoCancelDialog();
        OrderBusiness.getBillDetail(mApiProxy, billId);
        layoutTripType.setmRightText(getString(R.string.trip_price));
        mStateLayout.setOnClickStateLayoutListener(new StateLayout.OnClickStateLayoutListener() {
            @Override
            public void onClick(StateLayout.State state) {
                OrderBusiness.getBillDetail(mApiProxy, billId);
            }
        });
        return view;
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public void onTopbarLeftPress() {
        getActivity().finish();
    }

    @Override
    public void onTopbarRightPress() {
       // ExplainActivity.startActivityBy(getActivity(), ExplainActivity.SHOW_INFO_ABOUT_FEE_DESCRIPTION);
        if (notice!=null) {
            Intent intent = new Intent(getHostActivity(), ExplainActivity.class);
            intent.putExtra(ExplainActivity.KEY_CODE, ExplainActivity.SHOW_INFO_ABOUT_FEE_DESCRIPTION);
            intent.putParcelableArrayListExtra("notice", notice);
            ExplainActivity.startActivityBy(getHostActivity(), intent);

        }
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig customUiConfig = new CustomUiConfig();
        customUiConfig.mTopbarMiddleAreaName = getString(R.string.transaction_detail);
        customUiConfig.mTopbarRightAreaName = getString(R.string.help_icon);
        customUiConfig.mTopbarLeftAreaName = getString(R.string.back_icon);
        customUiConfig.mTopbarStyle = CustomUiConfig.BarStyle.WHITE_STYLE;
        customUiConfig.mRightStyle= CustomUiConfig.AreaStyle.ICON_STYLE;
        return customUiConfig;
    }

    @Override
    public boolean onProxyResult(ProxyResult<?> result) {
        CtApiResponse response = (CtApiResponse) result.getData();
        if (ApiManager.BILL_DETAIL.equals(response.getApiName())) {
            if (response != null && response.code == CtResponseCode.RESULT_OK) {
                hideNoCancelDialog();
                mStateLayout.changeState(StateLayout.State.INIT);
                if (response.result instanceof BillDetail) {
                    BillDetail billDetail = (BillDetail) response.result;
                    notice = billDetail.notice;
                    renderDetail(billDetail);
                }
                return true;
            } else {
                mStateLayout.changeState(StateLayout.State.NoNetwork);
                hideNoCancelDialog();
                String msg;
                if (response != null && !TextUtils.isEmpty(response.msg)) {
                    msg = response.msg;
                } else {
                    msg = PlatformUtil.getInstance().getString(R.string.feedback_data_err);
                }
                MessageUtils.showToast(msg);
                return false;
            }
        }

        return false;
    }

    private void renderDetail(BillDetail billDetail) {
        //total费用
        layoutOrderPrice.setmRightText(NumberUtils.getMoneyFormat(billDetail.orderPrice));
        //tranction fee
        if (BillDetail.HIDE.equals(billDetail.tranFeeIsCharged)){
            layoutTranFee.setVisibility(View.GONE);
        }else if (BillDetail.NOT_CHARGE.equals(billDetail.tranFeeIsCharged)){
            SpannableString ss = getFormatMoney(billDetail.tranFee);
            layoutTranFee.setmRightText(ss);
        }else{
            layoutTranFee.setmRightText(NumberUtils.getMoneyFormat(billDetail.tranFee));
        }
        //service fee
        if (BillDetail.HIDE.equals(billDetail.serviceFeeIsCharged)){
            layoutServiceFee.setVisibility(View.GONE);
        }else if (BillDetail.NOT_CHARGE.equals(billDetail.serviceFeeIsCharged)){
            SpannableString ss = getFormatMoney(billDetail.serviceFee);
            layoutServiceFee.setmRightText(ss);
        }else{
            layoutServiceFee.setmRightText(NumberUtils.getMoneyFormat(billDetail.serviceFee));
        }


        layoutTripTitle.setmRightText(billDetail.title);
        layoutUserNick.setmRightText(billDetail.userNick);
        layoutTime.setmRightText(
                TimeUtil.formatTime(
                        InputTime.build(billDetail.gmtCreated),
                        OutputTime.getLocalInstance(OutputTime.OutputType.Type_Minute))
        );

        moneyCount.setText(NumberUtils.getMoneyFormat(billDetail.money));
        moneyType.setText(billDetail.moneyType);

    }
    private SpannableString getFormatMoney( String price) {
        price = NumberUtils.getMoneyFormat(price);
        SpannableString ss = new SpannableString(price);
        ss.setSpan(new StrikethroughSpan(), 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return ss;
    }

    public static class BillDetail implements Serializable{

        public static final String HIDE="-1";
        public static final String NOT_CHARGE="0";
        public static final String CHARGE="1";
        public String serviceFee;//":public String0.0",
        public String gmtCreated;//1440400758000",
        public String title;//肥美的世界",
        public String payFee;//0.0",
        public String tranFee;
        public String userNick;//android",
        public String money;//10000.0,
        public String accountType;//: 1,
        public String orderPrice;//":public String 926.54",
        public String moneyType;//":public String TWD"
        public String serviceFeeIsCharged;
        public String tranFeeIsCharged;
        public ArrayList<FeeDescription> notice=new ArrayList<>();


    }
}
